<template>
  <div class="html">
    <span class="city-title-letter" id="hotCities">热门城市</span>
    <!-- 热门城市 -->
    <div class="hotcity">
      <span
        v-for="item of hotcity"
        :key="item.name"
        class="hotcitylist"
        @click="selectcity(item.cityId, item.name)"
      >
        {{ item.name }}
      </span>
    </div>
    <!-- 城市列表 -->
    <div v-for="(item, index) in citydata" :key="item.cityId">
      <span class="city-title-letter" v-if="index !== 0" :id="item.prefix">
        {{ item.prefix.toUpperCase() }}
      </span>
      <li
        v-for="i in item.cities"
        :key="i.cityId"
        @click="selectcity(i.cityId, i.name)"
      >
        {{ i.name }}
      </li>
    </div>
    <!-- 右侧导航 -->
    <div class="right-nav">
      <a v-for="item in citydata" :key="item.cityId" :href="'#' + item.prefix">
        {{ item.prefix === "hotCities" ? "热门" : item.prefix.toUpperCase() }}
      </a>
    </div>
  </div>
</template>
<script>
import { getCityDataApi } from "../utils/api";
export default {
  data() {
    return {
      citydata: [],
      hotcity: [],
    };
  },
  async beforeRouteEnter(to, from, next) {
    const res = await getCityDataApi();
    next((vm) => {
      vm.citydata = res.result;
      vm.hotcity = res.result[0].cities;
    });
  },
  methods: {
    selectcity(cityid, cityname) {
      localStorage.setItem("cityid", cityid);
      localStorage.setItem("cityname", cityname);
      this.$router.push("/index");
    },
  },
};
</script>

<style lang="less" scoped>
@import "../assets/css/var.less";
.html {
  padding-right: 15px;
  background: @border-color;
  font-size: 14px;
}
.hotcity {
  background-color: #f5f5f5;
  padding-bottom: 8px;
  padding-right: 30px;
  display: flex;
  flex-wrap: wrap;
  .hotcitylist {
    background: #fff;
    width: 95px;
    height: 33px;
    margin-top: 15px;
    margin-left: 4%;
    padding: 0 4px;
    border: 1px solid #e6e6e6;
    border-radius: 3px;
    line-height: 33px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.city-title-letter {
  padding-left: 25px;
  height: 30px;
  line-height: 30px;
  color: #333;
}
li {
  color: #333;
  height: 45px;
  line-height: 45px;
  padding-left: 15px;
  border-bottom: 1px solid #c8c7cc;
  background-color: #f5f5f5;
}
.right-nav {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  height: 70%;
  position: fixed;
  right: 0;
  top: 80px;
}
</style>
